/**
 * 在数组中值唯一并且有序的数组中
 * @param {*} arr 
 * @param {*} key 
 * @returns 
 */
function binarySearch(arr, key) {
  var low = 0;
  var high = arr.length - 1;
  while (low <= high) {
    var mid = parseInt((low + high) / 2);
    if (key < arr[mid]) {
      high = mid - 1;
    } else if (key > arr[mid]) {
      low = mid + 1;
    } else {
      return mid;
    }
  }
  return -1;
}

const arr = [1, 4, 5, 6, 7, 8, 10, 11, 23, 42, 44, 54, 56, 77, 102]
console.log(binarySearch(arr, 44))
console.log(binarySearch(arr, 1))
console.log(binarySearch(arr, 102))
console.log(binarySearch(arr, 1111))